#!/bin/sh

uci set system.@system[0].hostname='lihome'
uci -q batch <<-EOI
        set system.@system[0].timezone='CST-8'
        set system.@system[0].zonename='Asia/Shanghai'

        delete system.ntp.server
        add_list system.ntp.server='ntp1.aliyun.com'
        add_list system.ntp.server='ntp.tencent.com'
        add_list system.ntp.server='ntp.ntsc.ac.cn'
        add_list system.ntp.server='time.apple.com'
EOI
uci commit system

# ----------------------------------------------------------------
#
#root_password="password"
#lan_ip_address="192.168.1.254"
#lan_gateway_ip=""
#dns_server="223.6.6.6 2400:3200::1"
#
#pppoe_username="nbdl03966126"
#pppoe_password="zj120015"
# wlan_name="lihome"
# wlan_password="password"
#

# log potential errors
exec >/tmp/setup.log 2>&1

if [ -n "$root_password" ]; then
  (echo "$root_password"; sleep 1; echo "$root_password") | passwd > /dev/null
fi

# Configure LAN
# More options: https://openwrt.org/docs/guide-user/base-system/basic-networking
if [ -n "$lan_ip_address" ]; then
  uci set network.lan.ipaddr="$lan_ip_address"
  uci commit network
fi
if [ -n "$lan_gateway_ip" ]; then
  uci set network.lan.gateway="$lan_gateway_ip"
  uci commit network
fi
if [ -n "$dns_server" ]; then
  uci set network.lan.dns="$dns_server"
  uci commit network
fi

# Configure PPPoE
#protocol_pppoe_ppp_over_ethernet
if [ -n "$pppoe_username" ] && [ -n "$pppoe_password" ]; then
  uci set network.wan.proto=pppoe
  uci set network.wan.username="$pppoe_username"
  uci set network.wan.password="$pppoe_password"
  uci commit network
fi

if [ -n "$wlan_name" ] && [ -n "$wlan_password" ]; then
  uci set wireless.@wifi-iface[0].ssid="$wlan_name"
  uci set wireless.@wifi-iface[0].encryption='psk2'
  uci set wireless.@wifi-iface[0].key="$wlan_password"
  uci commit wireless
fi

sed -i '/\"admin\/vpn\": {/i \
        \"admin\/nas\": {\
                \"title\": "NAS",\
                \"order\": 60,\
                \"action\": {\
                        \"type\": \"firstchild\",\
                        \"recurse\": true\
                }\
        },\n' /usr/share/luci/menu.d/luci-base.json
sed -i 's/services/nas/g' /usr/share/luci/menu.d/luci-app-samba4.json
sed -i 's/services/nas/g' /usr/share/luci/menu.d/luci-app-minidlna.json

sed -i 's#downloads.openwrt.org#mirrors.aliyun.com\/openwrt#g' /etc/opkg/distfeeds.conf
sed -i '/openwrt_helloworld/d' /etc/opkg/distfeeds.conf
sed -i '/passwall/d' /etc/opkg/distfeeds.conf
sed -i 's/root::0:0:99999:7:::/root:$1$V4UetPzk$CYXluq4wUazHjmCDBCqXF.:0:0:99999:7:::/g' /etc/shadow
sed -i 's/root:::0:99999:7:::/root:$1$V4UetPzk$CYXluq4wUazHjmCDBCqXF.:0:0:99999:7:::/g' /etc/shadow

sed -i "s/# //g" /etc/opkg/distfeeds.conf

sed -i '/check_signature/d' /etc/opkg.conf

sed -i '/REDIRECT --to-ports 53/d' /etc/firewall.user

sed -i '/option disabled/d' /etc/config/wireless
sed -i '/set wireless.radio${devidx}.disabled/d' /lib/wifi/mac80211.sh

sed -i '/DISTRIB_REVISION/d' /etc/openwrt_release
echo "DISTRIB_REVISION='v23.05.5'" >> /etc/openwrt_release
sed -i '/DISTRIB_DESCRIPTION/d' /etc/openwrt_release
echo "DISTRIB_DESCRIPTION='LiWrt '" >> /etc/openwrt_release

sed -i '/log-facility/d' /etc/dnsmasq.conf
echo "log-facility=/dev/null" >> /etc/dnsmasq.conf

rm -rf /tmp/luci-modulecache/
rm -f /tmp/luci-indexcache

exit 0